package com.csstu.tomcat.servlet;

import com.csstu.tomcat.http.CsRequest;
import com.csstu.tomcat.http.CsResponse;
import com.csstu.tomcat.utils.WebUtils;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @program: cstomcat
 * @description: 程硕httpServlet子类
 * @author: 程硕
 * @create: 2025-08-16 18:03
 **/
public class CsCalServlet extends CsHttpServlet{
    @Override
    public void doGet(CsRequest request, CsResponse response) {
        //业务代码，完成计算任务
        int num1 = WebUtils.parseInt(request.getParameter("num1"), 0);
        int num2 = WebUtils.parseInt(request.getParameter("num2"), 0);

        int sum = num1 + num2;

        //返回结果
        OutputStream outputStream = response.getOutputStream();
        String respMes = CsResponse.responseHead
                + "<h1>" + num1 + " + " + num2 + " = "+ sum + "</h1>";
        //outputStream.write(().getBytes());
        try {
            outputStream.write(respMes.getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void doPost(CsRequest request, CsResponse response) {
        this.doGet(request,response);
    }

    @Override
    public void init() throws Exception {

    }

    @Override
    public void destroy() {

    }
}
